//
//  PlayCGIV4Parser.m
//  SuperPlayer
//
//  Created by cui on 2019/12/25.
//  Copyright © 2019 annidy. All rights reserved.
//

#import "SPPlayCGIParser_V4.h"
#import "SPSubStreamInfo.h"
#import "CTAdaptiveStream.h"
#import "TXImageSprite.h"
#import "CTSuperPlayerUtil.h"

@implementation SPPlayCGIParser_V4

+ (SPPlayCGIParseResult *)parseResponse:(NSDictionary *)dict {
    SPPlayCGIParseResult *ret = [[SPPlayCGIParseResult alloc] init];

    // 获取媒体信息
    NSDictionary *media = dict[@"media"];
    if ([media isKindOfClass:[NSDictionary class]]) {
        // 解析视频名称
        ret.name = [CTSuperPlayerUtil parseStringValue:[media valueForKeyPath:@"basicInfo.name"]];
        NSDictionary *streamInfo = [media valueForKeyPath:@"streamingInfo.plainOutput"];
        if (streamInfo == nil || ![streamInfo isKindOfClass:[NSDictionary class]]) {
            return ret;
        }

        NSNumber *duration = [media valueForKeyPath:@"basicInfo.duration"];
        if ([duration isKindOfClass:[NSNumber class]]) {
            ret.originalDuration = duration.doubleValue;
        }

        // 解析分辨率名称
        NSArray *subStreamDictArray = [CTSuperPlayerUtil parseArrayValue:streamInfo[@"subStreams"]];
        NSMutableArray *subStreamInfoArray = [NSMutableArray arrayWithCapacity:subStreamDictArray.count];
        for (NSDictionary *resInfo in subStreamDictArray) {
            CTSuperPlayerUrl *url = [[CTSuperPlayerUrl alloc] init];
            url.title = [CTSuperPlayerUtil parseStringValue:resInfo[@"resolutionName"]];
            // SPSubStreamInfo *info = [SPSubStreamInfo infoWithDictionary:resInfo];
            [subStreamInfoArray addObject:url];
        }
        ret.multiVideoURLs = subStreamInfoArray;

        // 解析视频播放url
        NSString *url = [CTSuperPlayerUtil parseStringValue:streamInfo[@"url"]];
        if ([url isKindOfClass:[NSString class]] && url.length > 0) {
            // 未加密直接解析出视频url
            ret.url = url;
        } else {
            // 有加密，url为空，则解析drm加密的url信息
            NSArray *urlArray = streamInfo[@"drmUrls"];
            if ([urlArray isKindOfClass:[NSArray class]] && urlArray.count > 0) {
                NSMutableArray *drmURLArray = [NSMutableArray arrayWithCapacity:urlArray.count];
                for (NSDictionary *dict in urlArray) {
                    if (![dict isKindOfClass:[NSDictionary class]]) {
                        continue;
                    }

                    CTAdaptiveStream *stream = [[CTAdaptiveStream alloc] init];
                    stream.url = [CTSuperPlayerUtil parseStringValue:dict[@"url"]];
                    stream.drmType = [CTSuperPlayerUtil parseStringValue:dict[@"type"]];
                    [drmURLArray addObject:stream];
                }
                ret.adaptiveStreamArray = drmURLArray;
            }
        }

        // 解析雪碧图信息
        NSDictionary *imageSpriteInfo = media[@"imageSpriteInfo"];
        if ([imageSpriteInfo isKindOfClass:[NSDictionary class]]) {
            NSString *vttURLString = [CTSuperPlayerUtil parseStringValue:imageSpriteInfo[@"webVttUrl"]];
            NSURL *vttURL = [NSURL URLWithString:vttURLString];
            NSArray *imageURLStrings = [CTSuperPlayerUtil parseArrayValue:imageSpriteInfo[@"imageUrls"]];
            NSMutableArray<NSURL *> *imageURLs = [NSMutableArray arrayWithCapacity:imageURLStrings.count];
            for (NSString *urlString in imageURLStrings) {
                NSURL *url = [NSURL URLWithString:urlString];
                if (url) {
                    [imageURLs addObject:url];
                }
            }

            TXImageSprite *sprite = [[TXImageSprite alloc] init];
            [sprite setVTTUrl:vttURL imageUrls:imageURLs];
            ret.imageSprite = sprite;
        }

        // 解析关键帧信息
        NSDictionary *keyFrameDescInfo = media[@"keyFrameDescInfo"];
        if ([keyFrameDescInfo isKindOfClass:[NSDictionary class]]) {
            NSArray *keyFrameDescList = keyFrameDescInfo[@"keyFrameDescList"];
            if ([keyFrameDescList isKindOfClass:[NSArray class]] && keyFrameDescList.count > 0) {
                NSMutableArray<SPVideoFrameDescription *> *videoPoints = [NSMutableArray array];
                for (NSDictionary *jsonObject in keyFrameDescList) {
                    SPVideoFrameDescription *point = [SPVideoFrameDescription instanceFromDictionary:jsonObject];
                    if (point) {
                        [videoPoints addObject:point];
                    }
                }
                ret.keyFrameDescList = videoPoints;
            }
        }
    }
    return ret;
}

@end
